package cn.tedu.submarine;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
//整个游戏世界
public class World extends JPanel {
    public static final int WIDTH = 641;
    public static final int HEIGHT = 479;

    public static final int RUNNING = 0;
    public static final int GAME_OVER = 1;
    private int state = RUNNING;

    private Battleship ship = new Battleship(); //战舰
    private SeaObject[] submarines = {}; //潜艇(侦察潜艇、鱼雷潜艇、水雷潜艇)
    private Mine[] mines = {}; //水雷
    private Bomb[] bombs = {}; //深水炸弹


    public SeaObject nextSubmarine(){
        Random rand = new Random();
        int type = rand.nextInt(20);
        if(type<10){
            return new ObserveSubmarine();
        }else if(type<15){
            return new TorpedoSubmarine();
        }else{
            return new MineSubmarine();
        }
    }

    private int subEnterIndex = 0;
    public void submarineEnterAction(){ //每10毫秒走一次
        subEnterIndex++;
        if(subEnterIndex%40==0){
            SeaObject obj = nextSubmarine();
            submarines = Arrays.copyOf(submarines,submarines.length+1);
            submarines[submarines.length-1] = obj;
        }
    }

    private int mineEnterIndex = 0;
    /** 雷(鱼雷、水雷)入场 */
    public void MineEnterAction(){
        mineEnterIndex++;
        if(mineEnterIndex%100==0){
            for(int i=0;i<submarines.length;i++){
                if(submarines[i] instanceof MineSubmarine){
                    MineSubmarine ms = (MineSubmarine)submarines[i];
                    Mine obj = ms.shootMine();
                    mines = Arrays.copyOf(mines,mines.length+1);
                    mines[mines.length-1] = obj;
                }
            }
        }
    }

    public void moveAction(){
        for(int i=0;i<submarines.length;i++){
            submarines[i].move();
        }
        for(int i=0;i<mines.length;i++){
            mines[i].move();
        }
        for(int i=0;i<bombs.length;i++){
            bombs[i].move();
        }
    }

    //删除越界的对象
    public void outOfBoundsAction(){
        for(int i=0;i<submarines.length;i++){
            if(submarines[i].isOutOfBounds() || submarines[i].isDEAD()){
                submarines[i] = submarines[submarines.length-1];
                submarines = Arrays.copyOf(submarines,submarines.length-1);
            }
        }

        for(int i=0;i<mines.length;i++){
            if(mines[i].isOutOfBounds() || mines[i].isDEAD()){
                mines[i] = mines[mines.length-1];
                mines = Arrays.copyOf(mines,mines.length-1);
            }
        }

        for(int i=0;i<bombs.length;i++){
            if(bombs[i].isOutOfBounds() || bombs[i].isDEAD()){
                bombs[i] = bombs[bombs.length-1];
                bombs = Arrays.copyOf(bombs,bombs.length-1);
            }
        }
    }

    private int score = 0; //玩家得分
    //深水炸弹与潜艇的碰撞
    public void bombBangAction(){
        for(int i=0;i<bombs.length;i++){
            Bomb b = bombs[i];
            for(int j=0;j<submarines.length;j++){
                SeaObject s = submarines[j];
                if(b.isLive() && s.isLive() && s.isHit(b)){
                    b.goDEAD();
                    s.goDEAD();
                    if(s instanceof EnemyScore){
                        EnemyScore es = (EnemyScore)s;
                        score += es.getScore();
                    }
                    if(s instanceof EnemyLife){
                        EnemyLife ea = (EnemyLife)s;
                        int num = ea.getLife();
                        ship.addLife(num);
                    }
                }
            }
        }
    }

    //水雷与战舰的碰撞
    public void mineBangAction(){
        for(int i=0;i<mines.length;i++){
            Mine m = mines[i];
            if(m.isLive() && ship.isLive() && m.isHit(ship)){
                m.goDEAD();
                ship.subtractLife();
            }
        }
    }

    /** 检测游戏结束 */
    public void checkGameOverAction(){
        if(ship.getLife()<=0){
            state = GAME_OVER;
        }
    }

    public void action(){
        KeyAdapter k = new KeyAdapter(){
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode() == KeyEvent.VK_SPACE){
                    Bomb obj = ship.shoot();
                    bombs = Arrays.copyOf(bombs,bombs.length+1);
                    bombs[bombs.length-1] = obj;
                }
                if(e.getKeyCode() == KeyEvent.VK_LEFT){
                    ship.moveLeft();
                }
                if(e.getKeyCode() == KeyEvent.VK_RIGHT){
                    ship.moveRight();;
                }
            }
        };
        this.addKeyListener(k);

        Timer timer = new Timer();
        int interval = 10;
        timer.schedule(new TimerTask() {
            public void run() {
                submarineEnterAction(); //潜艇入场
                MineEnterAction();      //水雷入场
                moveAction();           //动
                outOfBoundsAction();    //删除越界的对象
                bombBangAction();       //深水炸弹撞潜艇
                mineBangAction();       //水雷与战舰的碰撞
                checkGameOverAction();  //检测游戏结束
                repaint();              //重画
            }
        }, interval, interval);
    }

    public void paint(Graphics g){
        switch(state){
            case GAME_OVER:
                Images.gameover.paintIcon(null,g,0,0);
                break;
            case RUNNING:
                Images.sea.paintIcon(null,g,0,0); //画海洋图
                ship.paintImage(g);
                for(int i=0;i<submarines.length;i++){
                    submarines[i].paintImage(g);
                }
                for(int i=0;i<mines.length;i++){
                    mines[i].paintImage(g);
                }
                for(int i=0;i<bombs.length;i++){
                    bombs[i].paintImage(g);
                }
                g.drawString("SCORE: "+score,200,50);
                g.drawString("LIFE: "+ship.getLife(),400,50);
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        World world = new World();
        world.setFocusable(true);
        frame.add(world);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(WIDTH+16, HEIGHT+39);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        world.action();
    }
}





















/*import javax.swing.JFrame;

/*public class World extends JPanel {
    public static final int WIDTH = 641;
    public static final int HEIGHT = 479;

    private Battleship ship = new Battleship();

    private SeaObject[] submarines = {};

    private Mine[] mines = {};

    private Bomb[] bombs = {};

    public  void action(){
        Timer timer=new Timer();
        int interval =10;
        timer.schedule(new TimerTask() {
            @Override
            public void run() {

            }

            public void paint(Graphics g) {
                Images.sea.paintIcon(null, g, 0, 0);
                ship.paintImage(g);
                for (int i = 0; i < submarines.length; i++) {
                    submarines[i].paintImage(g);
                }

                for (int i = 0; i < mines.length; i++) {
                    mines[i].paintImage(g);
                }
                for (int i = 0; i < bombs.length; i++) {
                    bombs[i].paintImage(g);
                }
            }


        public static void main(String[] args) {
            JFrame frame = new JFrame();
            World world = new World();
            world.setFocusable(true);
            frame.add(world);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(WIDTH + 16, HEIGHT + 39);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
            world.action();


        }

}}


//因为将潜艇分装在了三个不同的数组中，所以得需要3个for
        /*SeaObject[] submarines= new SeaObject[5];
        submarines[0]=new ObserveSubmarine();
        submarines[1]=new ObserveSubmarine();
        submarines[2]=new TorpedoSubmarine();
        submarines[3]=new TorpedoSubmarine();
        submarines[4]=new MineSubmarine();
        for (int i = 0; i <submarines.length ; i++) {
            //侦察潜艇移动
            //侦察潜艇被深水炸弹射击
            System.out.println(submarines[i].x+","+submarines[i].y);
            submarines [i].move();


        }
        TorpedoSubmarine[] tses= new TorpedoSubmarine[2];

        tses[0]=new TorpedoSubmarine();
        tses[1]=new TorpedoSubmarine();
        for (int i = 0; i <tses.length ; i++) {
            tses[i]=new TorpedoSubmarine();
            System.out.println(tses[i].x+","+tses[i].y);
            tses[i].move();
        }

        MineSubmarine[] mses=new MineSubmarine[3];

        mses[0]=new MineSubmarine();
        mses[1]=new MineSubmarine();
        mses[2]=new MineSubmarine();
        for (int i = 0; i <mses.length ; i++) {
            mses[i] = new MineSubmarine();
            System.out.println(mses[i].x + "," + mses[i].y);
            mses[i].move();
        }
        Mine[] ms=new Mine[2];

        ms[0]=new Mine(180,200);
        ms[1]=new Mine(200,245);
        for (int i = 0; i < ms.length; i++) {
            System.out.println(ms[i].x+","+ms[i].y);
            ms[i].move();
            
        }
                
        Bomb[] bs=new Bomb[2];

        bs[0]=new Bomb(120,102);
        bs[1]=new Bomb(147,258);
            for (int i = 0; i <bs.length ; i++) {
                System.out.println(bs[i].x+","+bs[i].y);
                ms[i].move();
            }

            }















        /*Bomb[] b=new Bomb[3];// 创建Bomb数组对象
    b[0]=new Bomb(100,200);//创建Bomb对象
    b[1]=new Bomb(125,250);
    b[2]=new Bomb(200,300);

    b[0].x=111;//给第1个炸弹你的x修改为111
        System.out.println(b[1].width);//输出第2个炸弹得宽
    b[2].move();//第三个炸弹移动


















     /* Battleship b=new Battleship();
     ObserveSubmarine o1=new ObserveSubmarine();
     ObserveSubmarine o2=new ObserveSubmarine();
     ObserveSubmarine o3=new ObserveSubmarine();
     ObserveSubmarine o4=new ObserveSubmarine();
     TorpedoSubmarine t1=new TorpedoSubmarine();
     TorpedoSubmarine t2=new TorpedoSubmarine();
     MineSubmarine ms1=new MineSubmarine();
     MineSubmarine ms2=new MineSubmarine();
     Mine m1=new Mine(100,200);
     Mine m2=new Mine(125,250);
     Bomb b1=new Bomb(20,356);
     Bomb b2= new Bomb(147,245);
     System.out.println(b.width+","+b.height+","+b.x+","+b.speed+","+b.life);
     System.out.println(o1.width+","+o1.height+","+o1.x+","+o1.speed);
     System.out.println(o2.width+","+o2.height+","+o2.x+","+o2.speed);
     System.out.println(o3.width+","+o3.height+","+o3.x+","+o3.speed);
     System.out.println(o4.width+","+o4.height+","+o4.x+","+o4.speed);
     System.out.println(t1.width+","+t1.height+","+t1.x+","+t1.speed);
     System.out.println(t2.width+","+t2.height+","+t2.x+","+t2.speed);
     System.out.println(ms1.width+","+ms1.height+","+ms1.x+","+ms1.speed);
     System.out.println(ms2.width+","+ms2.height+","+ms2.x+","+ms2.speed);
     System.out.println(m1.width+","+m1.height+","+m1.x+","+m1.speed);
     System.out.println(m2.width+","+m2.height+","+m2.x+","+m2.speed);
    Bomb[]bs=new Bomb[3];
    bs[0]=new Bomb(100,200);
    bs[1]=new Bomb(200,300);
    bs[1].y=333;
     System.out.println();*/

